<?php

class LocationController extends Zend_Controller_Action {

    public function init() {
        $this->view->headTitle('MusicEvents')->headTitle('Location')->setSeparator(' :: ');
        $this->view->accordion = 1;
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->addActionContext('geo', 'json')
                ->addActionContext('events', 'json')
                ->initContext();
    }

    public function validateAction() {
        if ($this->getRequest()->isPost()) {
            $form = new Application_Form_Location();
            $this->view->accordion = 1;
            if ($this->getRequest()->isPost()) {
                $formData = $this->getRequest()->getPost();
                if ($form->isValid($formData)) {
                    $this->getRequest()->setDispatched();
                    $filter = new Zend_Filter_StripTags();
                    $location = $filter->filter($form->getValue('location'));
                    $within = $filter->filter($form->getValue('within'));
                    if ($within != "all")
                        $this->_redirect('location/' . urlencode($location) . '/' . urlencode($within));
                    else
                        $this->_redirect('location/' . urlencode($location));
                } else {
                    $form->populate($formData);
                    $this->view->locationForm = $form;
                }
            }
        }
    }

    public function showAction() {
        if ($this->getRequest()->has('location')) {
            $location = $this->_getParam('location');
            $within = $this->_getParam('within');
            $_location = Lastfm_Geo::getGeoLocation(urldecode($location));
            $this->view->location = $_location;
            $city = preg_split('/,/', $_location);
            $this->view->city = $city[0];
            $this->view->event_json = '/location/events/location/' . $location . '/within/' . $within;
            $this->view->event_tmp = '/template/artist';
            $form = new Application_Form_Location();
            $form->populate(array('location' => urldecode($location), 'within' => $within));
            $this->view->locationForm = $form;
            $this->view->headTitle(urldecode($location));
//            $response = $this->getResponse();
//            $response->insert('sidecontent', $this->view->render('location/side.phtml'));
        }
    }

    public function eventsAction() {
        if ($this->_request->isXmlHttpRequest()) {
            $location = ($this->getRequest()->has('location')) ? $this->_getParam('location') : null;
            $within = ($this->getRequest()->has('within')) ? $this->_getParam('within') : null;
            $lat = ($this->getRequest()->has('lat')) ? $this->_getParam('lat') : null;
            $lng = ($this->getRequest()->has('lng')) ? $this->_getParam('lng') : null;
            $page = $this->_getParam('page', 1);
            $limit = $this->_getParam('limit', 1);
            $events = Lastfm_Geo::getEvents($location, $within, $page, $limit, $lat, $lng);
            echo json_encode(Application_Model_Event::toArray($events));
            exit;
        }
    }

    public function geoAction() {
        if ($this->_request->isXmlHttpRequest()) {
            $soap = new SoapClient('http://services.getpos.de/clientObject.asmx?WSDL');
            $location = $soap->MyIPLocation(array('AccessId' => '524ef4cb-5915-4887-9a27-3442b0d1af13'));
            $result = $location->MyIPLocationResult;
            if ($result->Returncode == "200") {
                $getpos['returncode'] = $result->Returncode;
                $getpos['country'] = $result->Countryname;
                $getpos['region'] = $result->Regionname;
                $getpos['city'] = $result->Cityname;
                $getpos['position'] = array('latitude' => $result->Latitude, 'longitude' => $result->Longitude);
            } else {
                $getpos['returncode'] = $xml->returncode;
            }
            echo json_encode($getpos);
            exit;
        }
    }

}

